package com.stars.base.entity;

import com.alibaba.fastjson2.JSONObject;
import com.stars.base.constants.HttpStatusConstants;

/**
 * @author : Rocks
 * @version : 1.0
 * @date : 2022/10/28 12:48
 * @description : 返回结果集
 */
public class Result {

    private String code;
    private boolean result;
    private String msg;
    private Object data;

    public Result() {
    }

    public Result(String code, boolean result, String msg, Object data) {
        this.code = code;
        this.result = result;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Result success(){
        return success(null);
    }

    public static Result success(Object obj){
        return new Result(HttpStatusConstants.CODE_SUCCESS,true,"",obj);
    }

    public static Result error(){
        return error("");
    }

    public static Result error(String msg){
        return error(msg,null);
    }

    public static Result error(String msg,Object obj){
        return new Result(HttpStatusConstants.CODE_ERROR,false,msg,obj);
    }

    public static Result result(boolean result){
        return result ? success() : error();
    }

    public static Result result (boolean result,Object obj ,String msg) {
        return result? success(obj): error(msg);
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
